﻿
using Maticsoft.BLL;
using Maticsoft.Model;
using System;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Utils;

public partial class WebPage_Project_XinjianXiangmu : TeamBBasePage
{

    private Maticsoft.BLL.Team teams = new Maticsoft.BLL.Team();
    private ListItem teamType = null;
    private string youDate = "";

    private void addContent()
    {
        string fileName;
        string str3;
        int num;
        string str4;
        string str5;
        Exception exception;
        Upload upload = new Upload();
        string path = "";
        Maticsoft.Model.Team model = new Maticsoft.Model.Team();
        model.User_id = Convert.ToInt32(base.GetCookie("admin").ToString());
        if (this.projectType.SelectedItem.Value != "")
        {
            model.Team_type = this.projectType.SelectedItem.Value;
        }
        if (int.Parse(this.city.SelectedItem.Value) > -1)
        {
            model.City_id = int.Parse(this.city.SelectedItem.Value);
        }
        if (int.Parse(this.groupType.SelectedItem.Value) > -1)
        {
            model.Group_id = int.Parse(this.groupType.SelectedItem.Value);
        }
        model.Conduser = this.Conduse.SelectedItem.Value;
        model.Buyonce = this.Buyonce.SelectedItem.Value;
        model.Title = this.title.Value;
        model.Market_price = Convert.ToDecimal(this.market_price.Value);
        model.Team_price = Convert.ToDecimal(this.teamPrice.Value);
        model.Min_number = int.Parse(this.minnumber.Value);
        model.Max_number = int.Parse(this.maxnumber.Value);
        model.Per_number = int.Parse(this.pernumber.Value);
        DateTime dateTime = Helper.GetDateTime(this.begintime.Value, DateTime.MinValue);
        DateTime time2 = Helper.GetDateTime(this.endtime.Value, DateTime.MinValue);
        if ((dateTime != DateTime.MinValue) && (time2 != DateTime.MinValue))
        {
            TimeSpan span = (TimeSpan) (time2 - dateTime);
            if (this.expiretime.Value != "")
            {
                model.Expire_time = Convert.ToDateTime(this.expiretime.Value.ToString());
            }
            else
            {
                model.Expire_time = Convert.ToDateTime(this.begintime.Value.ToString());
            }
        }
        model.System = "N";
        model.Begin_time = Convert.ToDateTime(this.begintime.Value.ToString());
        model.End_time = Convert.ToDateTime(this.endtime.Value.ToString());
        model.Summary = this.createsummary.Value;
        model.Notice = this.creatnotice.Value;
        model.Sort_order = int.Parse(this.createsort_order.Value);
        if (int.Parse(this.shanghu.SelectedItem.Value) > -1)
        {
            model.Partner_id = int.Parse(this.shanghu.SelectedItem.Value);
        }
        model.Card = int.Parse(this.createcard.Value);
        model.Bonus = int.Parse(this.createbonus.Value);
        model.Product = this.createproduct.Value;
        if (((this.Image.FileName != null) || (this.Image1.FileName != null)) || (this.Image2.FileName != null))
        {
            path = upload.setUpload();
        }
        if ((this.Image.FileName != null) && (this.Image.FileName != ""))
        {
            if (this.Image.PostedFile.ContentLength > 0x7d000)
            {
                this.SetError("请上传 512KB 以内的个人肖像图片!");
                return;
            }
            fileName = this.Image.FileName;
            str3 = "";
            if (this.Image.FileName != "")
            {
                num = fileName.LastIndexOf(".");
                str4 = fileName.Substring(num);
                str3 = DateTime.Now.Ticks.ToString() + str4;
            }
            try
            {
                if (fileName != "")
                {
                    str5 = base.Server.MapPath(path);
                    if (!Directory.Exists(str5))
                    {
                        Directory.CreateDirectory(str5);
                    }
                    this.Image.PostedFile.SaveAs(str5 + str3);
                }
            }
            catch (Exception exception1)
            {
                exception = exception1;
                base.Response.Write(exception);
            }
            model.Image = path.Replace("~", "") + str3;
        }
        else
        {
            model.Image = "";
        }
        if ((this.Image1.FileName != null) && (this.Image1.FileName != ""))
        {
            if (this.Image1.PostedFile.ContentLength > 0x7d000)
            {
                this.SetError("请上传 512KB 以内的个人肖像图片!");
                return;
            }
            fileName = this.Image1.FileName;
            str3 = "";
            if (this.Image1.FileName != "")
            {
                num = fileName.LastIndexOf(".");
                str4 = fileName.Substring(num);
                str3 = DateTime.Now.Ticks.ToString() + 1 + str4;
            }
            try
            {
                if (fileName != "")
                {
                    str5 = base.Server.MapPath(path);
                    if (!Directory.Exists(str5))
                    {
                        Directory.CreateDirectory(str5);
                    }
                    this.Image1.PostedFile.SaveAs(str5 + str3);
                }
            }
            catch (Exception exception2)
            {
                exception = exception2;
                base.Response.Write(exception);
            }
            model.Image1 = path.Replace("~", "") + str3;
        }
        else
        {
            model.Image1 = "";
        }
        if ((this.Image2.FileName != null) && (this.Image2.FileName != ""))
        {
            if (this.Image2.PostedFile.ContentLength > 0x7d000)
            {
                this.SetError("请上传 512KB 以内的个人肖像图片!");
                return;
            }
            fileName = this.Image2.FileName;
            str3 = "";
            if (this.Image2.FileName != "")
            {
                num = fileName.LastIndexOf(".");
                str4 = fileName.Substring(num);
                str3 = DateTime.Now.Ticks.ToString() + 2 + str4;
            }
            try
            {
                if (fileName != "")
                {
                    str5 = base.Server.MapPath(path);
                    if (!Directory.Exists(str5))
                    {
                        Directory.CreateDirectory(str5);
                    }
                    this.Image2.PostedFile.SaveAs(str5 + str3);
                }
            }
            catch (Exception exception3)
            {
                exception = exception3;
                base.Response.Write(exception);
            }
            model.Image2 = path.Replace("~", "") + str3;
        }
        else
        {
            model.Image2 = "";
        }
        model.Flv = this.createflv.Value;
        if ((this.Reach_time.Value != null) && (this.Reach_time.Value != ""))
        {
            model.Reach_time = new DateTime?(Convert.ToDateTime(this.Reach_time.Value));
        }
        model.Detail = this.createdetail.Value;
        model.Userreview = this.createuserreview.Value;
        model.Systemreview = this.creatsystemreview.Value;
        model.Delivery = base.Request.Form["radio"].ToString();
        if (base.Request.Form["radio"].ToString() == "coupon")
        {
            try
            {
                model.Credit = Convert.ToInt32(this.createcredit.Value);
            }
            catch (Exception)
            {
                this.SetError("您输入的消费返利格式不正确,请重新输入");
            }
        }
        else
        {
            model.Credit = 0;
            try
            {
                model.Fare = Convert.ToInt32(this.createfare.Value);
            }
            catch (Exception)
            {
                this.SetError("您输入的快递费用格式不正确,请重新输入");
            }
            try
            {
                model.Farefree = int.Parse(this.createfarefree.Value);
            }
            catch (Exception)
            {
                this.SetError("您输入的名单数量格式不正确,请重新输入");
            }
            model.Express = this.createexpress.Value;
        }
        model.Mobile = this.createmobile.Value;
        model.Address = this.createaddress.Value;
        Maticsoft.BLL.Order order = new Maticsoft.BLL.Order();
        int count = order.GetModelList("State='pay' and Team_id=" + model.Id).Count;
        if (model.Conduser == "Y")
        {
            if (DateTime.Now > Convert.ToDateTime(this.endtime.Value))
            {
                if ((count > model.Min_number) && (model.State != "soldout"))
                {
                    model.State = "success";
                }
                else
                {
                    model.State = "failure";
                }
            }
            else if (count > model.Max_number)
            {
                model.State = "soldout";
            }
            else
            {
                model.State = "none";
            }
        }
        else
        {
            int num3 = 0;
            foreach (Maticsoft.Model.Order order2 in order.GetModelList("State='pay' and Team_id=" + model.Id))
            {
                num3 += order2.Quantity;
            }
            if (DateTime.Now > Convert.ToDateTime(this.endtime.Value))
            {
                if ((num3 > model.Min_number) && (model.State != "soldout"))
                {
                    model.State = "success";
                }
                else
                {
                    model.State = "failure";
                }
            }
            else if (num3 > model.Max_number)
            {
                model.State = "soldout";
            }
            else
            {
                model.State = "none";
            }
        }
        if (this.teams.Add(model) > 0)
        {
            base.SetSuccess("添加成功");
        }
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        this.HeaderControl1.Select = "program";
        if (!base.IsPostBack)
        {
            this.selectContent();
        }
        if (base.Request.HttpMethod == "POST")
        {
            if (base.GetCookie("admin") != string.Empty)
            {
                this.addContent();
                base.Response.Redirect("Project_DangqianXiangmu.aspx", true);
            }
            else
            {
                this.SetError("您没有登录，请登录");
                base.Response.Redirect("~/account/Login.aspx", true);
            }
        }
    }

    private void selectContent()
    {
        ListItem item2;
        this.createbonus.Value = base.GetMoney(base.ASSystem.Invitecredit);
        if (base.ASSystem.conduser == 0)
        {
            this.Conduse.SelectedIndex = 1;
        }
        this.begintime.Value = DateTime.Now.AddDays(1.0).ToString("yyyy-MM-dd");
        this.endtime.Value = DateTime.Now.AddDays(2.0).ToString("yyyy-MM-dd");
        this.expiretime.Value = DateTime.Now.AddMonths(3).ToString("yyyy-MM-dd");
        this.teamType = new ListItem();
        this.teamType.Text = "团购项目";
        this.teamType.Value = "normal";
        this.teamType.Selected = true;
        this.projectType.Items.Add(this.teamType);
        this.teamType = new ListItem();
        this.teamType.Text = "秒杀项目";
        this.teamType.Value = "seconds";
        this.projectType.Items.Add(this.teamType);
        this.teamType = new ListItem();
        this.teamType.Text = "热销商品";
        this.teamType.Value = "goods";
        this.projectType.Items.Add(this.teamType);
        this.projectType.DataBind();
        Maticsoft.BLL.Category category = new Maticsoft.BLL.Category();
        ListItem item = new ListItem();
        item.Text = "全部城市";
        item.Value = "0";
        item.Selected = true;
        this.city.Items.Add(item);
        foreach (Maticsoft.Model.Category category2 in category.GetModelList("Zone='City'"))
        {
            item2 = new ListItem();
            item2.Text = category2.Name;
            item2.Value = category2.Id.ToString();
            this.city.Items.Add(item2);
        }
        this.city.DataBind();
        ListItem item3 = new ListItem();
        item3.Text = "====请选择分类=====";
        item3.Value = "-1";
        item3.Selected = true;
        this.groupType.Items.Add(item3);
        foreach (Maticsoft.Model.Category category2 in category.GetModelList("Zone='group'"))
        {
            item2 = new ListItem();
            item2.Text = category2.Name;
            item2.Value = category2.Id.ToString();
            this.groupType.Items.Add(item2);
        }
        this.groupType.DataBind();
        Maticsoft.BLL.Partner partner = new Maticsoft.BLL.Partner();
        ListItem item4 = new ListItem();
        item4.Text = "===========请选择商户============";
        item4.Value = "-1";
        item4.Selected = true;
        this.shanghu.Items.Add(item4);
        foreach (Maticsoft.Model.Partner partner2 in partner.GetModelList(""))
        {
            item2 = new ListItem();
            item2.Text = partner2.Username;
            item2.Value = partner2.Id.ToString();
            this.shanghu.Items.Add(item2);
        }
        this.shanghu.DataBind();
    }
}

